package com.t2t.top.outmemory;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * 5. 使用unsafe分配本地内存
 * <p/>
 * VM Args:-Xms20m -Xmx20m -XX:MaxDirectMemorySize=10M
 */
public class DirectMemoryOOM {
    private static final int _1MB = 1024 * 1024;

    public static void main(String[] args) throws Exception {
        Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeField.get(null);
        while (true) {
            unsafe.allocateMemory(_1MB);
            Thread.sleep(1000);
        }
    }
}
